/*
 * 版权所有(c)  1952110078@qq.com  2022 - 2022
 */

package com.ldc.commonlibs.utils;

import android.content.Context;
import android.widget.ImageView;

import com.bumptech.glide.Glide;
import com.bumptech.glide.load.engine.DiskCacheStrategy;
import com.bumptech.glide.load.resource.bitmap.CenterCrop;
import com.bumptech.glide.load.resource.bitmap.CenterInside;
import com.bumptech.glide.load.resource.bitmap.CircleCrop;
import com.bumptech.glide.load.resource.bitmap.FitCenter;
import com.bumptech.glide.load.resource.bitmap.RoundedCorners;
import com.bumptech.glide.request.RequestOptions;

/**
 * @author liudc
 * @since 2022/7/20 22:13
 */
public class GlideUtil {

    public static void setImageView(ImageView imageView, String url) {
        if (imageView == null) {
            return;
        }
        setImageViewRadius(imageView, url, 0);
    }

    public static void setImageViewRadius(ImageView imageView, String url, float radius) {
        if (imageView == null) {
            return;
        }
        RequestOptions options = getRequestOptions()
                .transform(new CenterCrop(), getRoundedCorners(imageView.getContext(), radius));
        Glide.with(imageView)
                .load(url)
                .apply(options)
                .into(imageView);

    }

    public static void setImageViewCircleCrop(ImageView imageView, String url) {
        if (imageView == null) {
            return;
        }
        RequestOptions options = getRequestOptions()
                .transform(new CenterCrop(), getCircleCrop());
        Glide.with(imageView)
                .load(url)
                .apply(options)
                .into(imageView);

    }

    public static RoundedCorners getRoundedCorners(Context context, float radius) {
        return new RoundedCorners(DpUtil.dp2px(context, radius));
    }

    public static CircleCrop getCircleCrop() {
        return new CircleCrop();
    }

    public static RequestOptions getRequestOptions() {
        return RequestOptions.diskCacheStrategyOf(DiskCacheStrategy.NONE).skipMemoryCache(true);
    }
}
